home *** CD-ROM | disk | FTP | other *** search
- #define IGNORE_STDIO_STUBS
- #define __string_h
-
- #ifdef OLDGCC
-
- #include <Common.h>
- #include <System/SysAll.h>
- #include <UI/UIAll.h>
- #include <Unix/sys_types.h>
-
- #else
-
- #include <PalmOS.h>
- #include <PalmCompatibility.h>
- #include <Unix/sys_types.h>
-
- #endif
-
- #include "stringil.h"
- #include "stdio2.h"
-
- struct prchead {
- char name[32]; //0-32
- short int attr; //32-33
- short int vers; //34-35
- long int cr, md, bkt; //times 36-47
- long int mn, app, sort; // zero 48-59 - zero for prcs.
- long int type, crea; //60-67
- long int uidseed, nxrec; //68-75 - uidseed rand, nxrec zero;
- short int nrecs; //76-78
- } head;
-
- struct rsrcdbent {
- long int rsrc;
- short int rsid;
- long int ofst; //80+10*(nrecs+index)
- } rsrcent, rsrcnxt;
-
- struct normdbent {
- long int ofst, uid; //80+8*(nrecs+index)
- } dataent, datanxt;
-
- int MakeDatabase(FILE * fd, int destruct)
- {
- FILE *tfd;
- LocalID lid, aiid, siid;
- int i;
- UInt16 cardno = 0;
- UInt16 num, attr;
- UInt32 uid, asz, ssz, ofst, count, total;
- DmOpenRef db;
- void *ap;
- char buf[8];
- RectangleType r;
-
- r.topLeft.x = 0, r.topLeft.y = 15, r.extent.x = 160, r.extent.y = 30;
-
- WinEraseRectangle(&r, 0);
- r.topLeft.y = 30, r.extent.y = 15;
-
- FileTell(fd, &total, NULL);
- if( destruct )
- FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
-
- memset(&head, 0, sizeof(head));
- fread(&head, sizeof(head), 1, fd);
-
- count = sizeof(head);
-
- r.extent.x = 160 * count / total;
- WinDrawRectangle(&r, 0);
-
- WinDrawChars(head.name, strlen(head.name), 0, 15);
-
- if( DmCreateDatabase(cardno, head.name, head.crea, head.type,
- (head.attr & dmHdrAttrResDB)) ) {
- lid = DmFindDatabase(cardno, head.name);
-
- /////////////////CONFIRM
-
- if (lid)
- DmDeleteDatabase(cardno, lid);
- DmCreateDatabase(cardno, head.name, head.crea, head.type,
- (head.attr & dmHdrAttrResDB)) ;
- }
-
- lid = DmFindDatabase(cardno, head.name);
-
- if (!lid) {
- fclose(fd);
- return 1;
- }
-
- head.attr &= ~dmHdrAttrReadOnly;
- DmSetDatabaseInfo(cardno, lid, NULL, &head.attr, &head.vers, &head.cr,
- &head.md, &head.bkt, &head.mn, NULL, NULL, NULL, NULL);
-
- WinDrawChars("/", 1, 150, 0);
-
- {
- char buf[256];
-
- tfd = FileOpen(0, "MAKEDBtemporary", 'DATA', 'NZIP', fileModeReadWrite, NULL);
-
- if (head.attr & dmHdrAttrResDB) {
-
- ssz = sizeof(struct rsrcdbent) * head.nrecs;
- count += ssz;
-
- while( ssz ) {
- i = ssz > 256 ? 256 : ssz;
- fread(buf, 1, i , fd);
- fwrite(buf, 1, i, tfd);
- ssz -= i;
- }
-
- FileControl(fileOpDestructiveReadMode, tfd, NULL, NULL);
- fread(&rsrcent,1,sizeof(rsrcent),tfd);
-
- ofst = rsrcent.ofst;
-
-
- } else {
-
-
- ssz = sizeof(struct normdbent) * head.nrecs;
- count += ssz;
-
- while( ssz ) {
- i = ssz > 256 ? 256 : ssz;
- fread(buf, 1, i , fd);
- fwrite(buf, 1, i, tfd);
- ssz -= i;
- }
-
-
- FileControl(fileOpDestructiveReadMode, tfd, NULL, NULL);
- fread(&dataent,1,sizeof(dataent),tfd);
-
- ofst = dataent.ofst;
-
- }
-
- }
-
- r.extent.x = 160 * count / total;
- WinDrawRectangle(&r, 0);
-
-
- db = DmOpenDatabase(cardno, lid, dmModeReadWrite);
-
- asz = 0;
- ssz = 0;
- if (head.app)
- asz = (head.sort ? head.sort : ofst) - head.app;
- if (head.sort)
- ssz = ofst - head.sort;
-
- #define dmfread(bufP, objSize, numObj, stream) \
- FileDmRead((stream), bufP, 0, (objSize), (numObj), NULL)
-
- // fread(buf, 2, 1, fd);
-
-
- WinDrawChars("\\", 1, 155, 0);
-
- if (asz) {
- while (count < head.app)
- count += fread(buf, 1 , head.app - count > 8 ? 8 : head.app - count, fd);
- ap = DmNewHandle(db, asz);
- count += dmfread(MemHandleLock(ap), 1, asz, fd);
- MemHandleUnlock(ap);
- aiid = MemHandleToLocalID(ap);
- DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- &aiid, NULL, NULL, NULL);
-
- }
-
- if (ssz) {
- while (count < head.sort)
- count += fread(buf, 1, head.sort - count > 8 ? 8 : head.sort - count, fd);
- ap = DmNewHandle(db, ssz);
- count += dmfread(MemHandleLock(ap), 1, ssz, fd);
- MemHandleUnlock(ap);
- siid = MemHandleToLocalID(ap);
- DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, &siid, NULL, NULL);
- }
-
- r.extent.x = 160 * count / total;
- WinDrawRectangle(&r, 0);
-
- if (head.attr & dmHdrAttrResDB) {
- for (i = 0; i < head.nrecs; i++) {
-
- if( i < head.nrecs - 1 )
- fread(&rsrcnxt,1,sizeof(struct rsrcdbent),tfd);
- else
- rsrcnxt.ofst = total;
- ssz = rsrcnxt.ofst - rsrcent.ofst;
-
- ap = DmNewResource(db, rsrcent.rsrc, rsrcent.rsid, ssz);
- while (count < rsrcent.ofst)
- count += fread(buf, 1, rsrcent.ofst - count > 8 ? 8 : rsrcent.ofst - count, fd);
- count += dmfread(MemHandleLock(ap), 1, ssz, fd);
- MemHandleUnlock(ap);
- DmReleaseResource(ap);
-
- rsrcent = rsrcnxt;
-
- r.extent.x = 160 * count / total;
- WinDrawRectangle(&r, 0);
-
- }
-
- } else {
- for (i = 0; i < head.nrecs; i++) {
-
- if( i < head.nrecs - 1 )
- fread(&datanxt,1,sizeof(struct normdbent),tfd);
- else
- datanxt.ofst = total;
- ssz = datanxt.ofst - dataent.ofst;
-
- num = 0xffff;
- ap = DmNewRecord(db, &num, ssz);
- while (count < dataent.ofst)
- count += fread(buf, 1, dataent.ofst - count > 8 ? 8 : dataent.ofst - count, fd);
- count += dmfread(MemHandleLock(ap), 1, ssz, fd);
- MemHandleUnlock(ap);
-
- attr = dataent.uid >> 24;
- uid = dataent.uid & 0xffffffUL;
- DmSetRecordInfo(db, num, &attr, &uid);
- DmReleaseRecord(db, num, false);
-
- rsrcent = rsrcnxt;
-
- r.extent.x = 160 * count / total;
- WinDrawRectangle(&r, 0);
-
- }
-
- }
-
- DmCloseDatabase(db);
- fclose(tfd);
- unlink("MAKEDBtemporary");
- return 0;
-
- }
-